import { loginApi, userInfoApi } from '@/api/user'
import { delToken, getToken, setToken } from '@/utils/storage'
import { Message } from 'element-ui'
import router from '@/router'
export default {
  namespaced: true,
  state: {
    msg: {
      token: getToken() || '',
      userInfo: {}
    }
  },

  mutations: {
    getToken(state, token) {
      state.msg.token = token
      setToken(token)
    },
    delToken(state) {
      state.token = ''
      delToken()
    },
    setUserInfo(state, userMsg) {
      state.userInfo = userMsg
    }
  },

  actions: {
    async getUserInfo(ctx, data) {
      const res = await loginApi(data)
      ctx.commit('getToken', res.data.token)
      router.push('/')
      Message.success('登录成功')
    },
    clearToken(context) {
      context.commit('delToken')
    },
    async getIUserInfo(ctx) {
      const res = await userInfoApi()
      ctx.commit('setUserInfo', res.data)
      console.log(res.data)
    }
  },

  getters: {}
}

